package core;

import Utils.tempWorkTree;
import fileoperation.FileCreation;
import fileoperation.FileDeletion;
import gitobject.Commit;
import gitobject.Tree;
import repository.Repository;

import java.io.File;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.charset.StandardCharsets;

//返回上一次的commit
public class JitReset {

    
    public static void reset(String commitKey) throws IOException {
        tempWorkTree.updateWorkTree();
    
        FileWriter fileWriter = new FileWriter(tempWorkTree.indexLoc);
        fileWriter.write("");
        fileWriter.close();

        Commit commit = Commit.deserialize(commitKey);
        String treeKey = commit.getTree();
        Tree tree = Tree.deserialize(treeKey);
    
        FileCreation.recoverIndex(tree, tempWorkTree.workTree);
        
        String branchName = JitBranch.getBranchName(tempWorkTree.gitDir);
        FileOutputStream historyOut = new FileOutputStream(tempWorkTree.commitHistoryLoc+File.separator+branchName, true);
        String temp2=Commit.getLastCommit()+" "+commitKey+" "+branchName+" reset:moving to "+commitKey.substring(0,4)+"\n";
        historyOut.write(temp2.getBytes(StandardCharsets.UTF_8));
        historyOut.close();
        
        //上一次提交
    
        FileOutputStream curOut = new FileOutputStream(tempWorkTree.lastCommitLoc + File.separator + branchName, false);
        String temp = commitKey;
        curOut.write(temp.getBytes(StandardCharsets.UTF_8));
        curOut.close();

    }

    
    public static void resetHard(String commitKey) throws IOException {
        tempWorkTree.updateWorkTree();

        
        File file = new File(tempWorkTree.workTree);
        File[] files = file.listFiles();
        for (File file1 : files) {
            if(!file1.getName().equals(".jit"))
                FileDeletion.deleteFile(file1);
        }
        
        FileOutputStream fileOutputStream = new FileOutputStream(tempWorkTree.indexLoc, false);
        fileOutputStream.write("".getBytes(StandardCharsets.UTF_8));
        Commit commit = null;
        commit = Commit.deserialize(commitKey);
        String treeKey = commit.getTree();
        Tree tree = Tree.deserialize(treeKey);
        FileCreation.recoverIndex(tree, tempWorkTree.workTree);
        FileCreation.recoverWorkTree(tree, tempWorkTree.workTree);
        
        String branchName = JitBranch.getBranchName(tempWorkTree.gitDir);
        FileOutputStream fileOutputStream1 = new FileOutputStream(tempWorkTree.logHead, true);
        FileOutputStream historyOut = new FileOutputStream(tempWorkTree.commitHistoryLoc+File.separator+branchName, true);
        String temp2=Commit.getLastCommit()+" "+commitKey+" "+branchName+" reset:moving to "+commitKey.substring(0,4)+"\n";
        fileOutputStream1.write(temp2.getBytes(StandardCharsets.UTF_8));
        historyOut.write(temp2.getBytes(StandardCharsets.UTF_8));
        historyOut.close();
        fileOutputStream1.close();
        
        FileOutputStream curOut = new FileOutputStream(tempWorkTree.lastCommitLoc + File.separator + branchName, false);
        String temp = commitKey;
        curOut.write(temp.getBytes(StandardCharsets.UTF_8));
        curOut.close();

    }
    
    public static void resetHard(String commitKey,String check) throws IOException {
        FileOutputStream fileOutputStream = new FileOutputStream(tempWorkTree.indexLoc, false);
        fileOutputStream.write("".getBytes(StandardCharsets.UTF_8));
        Commit commit = null;
        commit = Commit.deserialize(commitKey);
        String treeKey = commit.getTree();
        Tree tree = Tree.deserialize(treeKey);
        FileCreation.recoverIndex(tree, tempWorkTree.workTree);
        FileCreation.recoverWorkTree(tree, tempWorkTree.workTree);
    }
}
